using BlogApi.Data;
using BlogApi.Models;
using Microsoft.EntityFrameworkCore;

namespace BlogApi.Interfaces;

public class BlogRepository<T> : IBlogRepository<T> where T : EntityBase
{
    private readonly ApiDbContext _db;
    private DbSet<T> _tb;

    public BlogRepository(ApiDbContext db)
    {
        _db = db;
        _tb = db.Set<T>();
    }

    public List<T> GetAllBlogs()
    {
        var list = _tb.ToList();
        return list;
    }

    public T? GetById(Guid id)
    {
        var blog = _tb.Find(id);
        return blog;
    }

    public T AddBlog(T entity)
    {
        _tb.Add(entity);
        _db.SaveChanges();
        return entity;
    }

    public T EditBlog(T entity)
    {
        _tb.Update(entity);
        _db.SaveChanges();
        return entity;
    }

    public T DelById(T entity)
    {
        _tb.Remove(entity);
        _db.SaveChanges();
        return entity;
    }
}